<?php
declare (strict_types = 1);

namespace app\service\logic\common\user;

use app\model\communal\UserExpertEvaluateModel;
use app\model\communal\UserExpertModel;
use app\service\logic\BaseService;
use think\facade\Db;

final class ExpertEvaluateService extends BaseService
{
    protected string $modelClassName = 'UserExpertEvaluateModel';

    /**
     * @param $params
     * @return mixed
     */
    public function create($params)
    {
        Db::connect('communal')->transaction(function () use ($params, &$model) {
            $model = parent::create($params);

            // 专家平均分搞里头
            $stars = UserExpertEvaluateModel::where('expert_id', $params['expert_id'])->avg('stars');
            UserExpertModel::where('id', $params['expert_id'])->update(['praise_weight' => round($stars)]);
        });
        return $model;
    }

    /**
     * @return array
     */
    public function satisfies()
    {
        $total = 0;
        // $whole = '';
        $options = $this->amisOptions(UserExpertEvaluateModel::$satisfyOptions, 'name', 'name');
        foreach ($options as $key=>$option) {
            $count = UserExpertEvaluateModel::withSearch(['satisfy_contents'], ['satisfy_contents' => $option['value']])->count();
            $options[$key]['number'] = $count;
            // $whole .= ',' . $option['value'];
            $total += $count;
        }
        // $total = UserExpertEvaluateModel::withSearch(['satisfy_contents'], ['satisfy_contents' => $whole])->count();
        // array_unshift($options, ['label' => '全部','value' => $whole, 'number' => $total]);
        // 不确定啥需求
        array_unshift($options, ['label' => '全部','value' => '', 'number' => $total]);
        return $options;
    }

    /**
     * 评价数量
     *
     * @param $expertId
     * @return int
     */
    public function getNumberByExpertId($expertId)
    {
        return UserExpertEvaluateModel::where('expert_id', $expertId)->count();
    }
}
